'''
有名管道: 
1. 有名管道就是利用磁盘文件进行进程间通信
2. 管道是单向的, 想双向双向就双管道
3. 命名管道必须以只读或者只写的模式打开，它不能以读+写的模式打开
'''


import os, time, sys
pipe_name = 'pipe_test'

def child( ):
    pipeout = os.open(pipe_name, os.O_WRONLY)
    counter = 0
    while True:
        time.sleep(1)
        os.write(pipeout, 'Number %03d\n' % counter)
        counter = (counter+1) % 5

def parent( ):
    pipein = open(pipe_name, 'r')
    while True:
        line = pipein.readline()[:-1]
        print 'Parent %d got "%s" at %s' % (os.getpid(), line, time.time( ))

if __name__ == '__main__':
    if not os.path.exists(pipe_name):
        os.mkfifo(pipe_name)  
    pid = os.fork()    
    if pid != 0:
        parent()
    else:       
        child()